import { ShapeFlags } from "../shared/ShapeFlags";

export function initSlots(instance, children) {
    const { vnode } = instance;
    if (vnode.shapeFlag & ShapeFlags.SLOT_CHILDREN) {
        normalizeObjectSlots(children, instance.slots);
    }
}

// 具名插槽
function normalizeObjectSlots(children, slots) {
    for (const key in children) {
        const value = children[key];

        // value = slot
        slots[key] = (props) => normalizeSlotValue(value(props));
    }
}

// 匿名插槽
function normalizeSlotValue(value) {
    if (Array.isArray(value)) {
        return value;
    }

    return [value];
}